﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using StreetlightGames.SpaceShooter.Patterns;
using System.IO;
using System.Xml;

namespace StreetlightGames.SpaceShooter.WaveMetaData
{
    [DataContract]
    public class ShipGroup
    {
        /// <summary>Gets or sets the StartDelay value</summary>
        [DataMember]
        public double StartDelay
        {
            get { return _startDelay; }
            set { _startDelay = value; }
        }
        private double _startDelay;

        /// <summary>Gets or sets the Interval value</summary>
        [DataMember]
        public double Interval
        {
            get { return _interval; }
            set { _interval = value; }
        }
        private double _interval;

        /// <summary>Gets or sets the StartPosition value</summary>
        [DataMember]
        public string StartPosition
        {
            get { return _startPosition; }
            set { _startPosition = value; }
        }
        private string _startPosition;

        /// <summary>Gets or sets the StartVelocity value</summary>
        [DataMember]
        public string StartVelocity
        {
            get { return _startVelocity; }
            set { _startVelocity = value; }
        }
        private string _startVelocity;

        /// <summary>Gets the Patterns value</summary>
        [DataMember]
        public List<PatternData> Patterns
        {
            get { return _patterns; }
            private set { _patterns = value; }
        }
        private List<PatternData> _patterns = new List<PatternData>();

        /// <summary>Gets or sets the Ships value</summary>
        [DataMember]
        public List<ShipData> Ships
        {
            get { return _ships; }
            set { _ships = value; }
        }
        private List<ShipData> _ships = new List<ShipData>();

        public ShipGroup()
        {
        }

        public void AddPattern(Pattern nextPattern)
        {
            Patterns.Add(new PatternData(nextPattern));
        }

        public Pattern CreatePattern()
        {
            Pattern pattern = null;
            Pattern nextPattern = pattern;
            foreach (PatternData patternData in Patterns)
            {
                if (nextPattern == null)
                {
                    pattern = patternData.CreatePattern();
                    nextPattern = pattern;
                }
                else
                {
                    nextPattern.NextPattern = patternData.CreatePattern();
                    nextPattern = nextPattern.NextPattern;
                }
            }
            return pattern;
        }

        public ShipGroup Duplicate()
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(ShipGroup));
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlWriter xw = new XmlTextWriter(sw);
            serializer.WriteObject(xw, this);
            StringReader sr = new StringReader(sb.ToString());
            XmlReader xr = new XmlTextReader(sr);
            ShipGroup shipGroup = serializer.ReadObject(xr) as ShipGroup;
            return shipGroup;
        }
    }
}
